useMutation
const {
data,
error,
status,
isPending,
isSuccess,
isError,
failureCount,
failureReason,
mutate,
mutateAsync,
resetMutation,
result,
} = useMutation(
{
mutationFn,
onSuccess,
onError,
onSettled,
retry,
retryDelay,
gcTime,
},
queryClient,
)
// Usage:
mutate(variables);
await mutateAsync(variables);
resetMutation();
const current = result(); // returns MutationObserverResult<T, P>
Parameter1 (Options)
mutationFn: Future<T> Function(P)- Required
- A function that performs the mutation and returns a
Future<T>. - Receives a
Pvalue (the mutation variables) and should return the mutation result.
onSuccess: void Function(T?)?- Optional
- Called with the mutation result when the mutation succeeds.
onError: void Function(Object?)?- Optional
- Called with the error when the mutation fails.
onSettled: void Function(T?, Object?)?- Optional
- Called when the mutation finishes either successfully or with an error.
retry: dynamic- Optional
- Retry configuration:
bool(true = infinite),int(max attempts), or a function(failureCount, error) => bool.
retryDelay: dynamic- Optional
- Delay between retries in milliseconds or a function
(attempt, error) => int.
gcTime: int?- Optional
- Garbage collection time in milliseconds for the mutation's cache entry. When
<= 0, GC is disabled. If omitted, the client default is used.
Parameter2 (QueryClient)
queryClient: QueryClient?- Optional. Use this to provide a custom
QueryClient; otherwise the one from the nearest context will be used.
- Optional. Use this to provide a custom
Returns
-
mutate: void Function(P variables)- Triggers the mutation and swallows errors (does not throw). Accepts an optional per-call
MutateOptions<T>when used via the lower-level observer API.
- Triggers the mutation and swallows errors (does not throw). Accepts an optional per-call
-
mutateAsync: Future<T> Function(P variables, [MutateOptions<T>? options])- Triggers the mutation and returns a
Futurethat resolves with the mutation result or throws on error.
- Triggers the mutation and returns a
-
resetMutation: void Function()- Resets the mutation state to its initial state.
-
result: MutationObserverResult<T, P> Function()- Returns the current
MutationObserverResultexposing the latest state.
- Returns the current
-
status: MutationStatus- One of
idle,pending,error, orsuccess.
- One of
-
isIdle,isPending,isSuccess,isError:bool- Convenience booleans derived from
status.
- Convenience booleans derived from
-
data: T?- Defaults to
null. - The last successfully resolved data for the mutation.
- Defaults to
-
error: Object?- Defaults to
null. - The error object for the mutation, if an error occurred.
- Defaults to
-
failureCount: int- The failure count for the current retry cycle.
-
failureReason: Object?- The last failure reason, if any.
Notes:
- The hook exposes a small wrapper (
MutationResult) around the underlying observer. Usemutatefor fire-and-forget semantics ormutateAsyncwhen you need aFuture. - The
variablesandsubmittedAtproperties from some JS docs are not currently surfaced in the Dart observer result; useresult()to access available fields.